package com.example.sls.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 编者：蓝江富
 * 时间：2024/6/16 15:47
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
  // 注册拦截器
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(new SaInterceptor(handler -> {
          // 根据路由划分模块，不同模块不同鉴权
          SaRouter.match("/users/**", r -> StpUtil.checkLogin());
          SaRouter.match("/admin/**", r -> StpUtil.checkLogin());
          SaRouter.match("/field/**", r -> StpUtil.checkLogin());
          SaRouter.match("/social/**", r -> StpUtil.checkLogin());
          SaRouter.match("/logistics/**", r -> StpUtil.checkLogin());
          // ...
      })).addPathPatterns(
              "/users/**",
              "/admin/**",
              "/field/**",
              "/social/**",
              "/logistics/**"
      );
  }
}

